<?php

/**
 * Created by PhpStorm.
 * User: u
 * Date: 2017/4/28
 * Time: 0:39
 */
class Sylot extends WebBase {

    public final function js_ssc() {
//         echo 1;
        $this->display('sylot/js/js_ssc.php');
    }

    public final function js_pk10() {
        $this->display('sylot/js/js_pk10.php');
    }

    public final function js_jsft() {
        $this->display('sylot/js/js_jsft.php');
    }

    public final function js_lhc() {
        $this->display('sylot/js/js_lhc.php');
    }

    public final function lhc() {
        $this->display('sylot/lhc.php');
    }
    
    public final function js_test() {
        $lastNo['actionNo'] = 20180915620;
        $keytype = 51;
        $sql = "select sum(money) as money,id,actionData,playedGroup,uid,odds from {$this->prename}bets where actionNo={$lastNo['actionNo']}  and type={$keytype} GROUP BY playedGroup,actionData  ORDER BY money desc";
        $data = $this->getRows($sql);
        $sql = "select sum(money) as money,playedGroup from {$this->prename}bets where actionNo={$lastNo['actionNo']}   and type={$keytype} GROUP BY playedGroup  ORDER BY money desc";
        $poistion_count = $this->getRows($sql);
        $res = [];
        foreach ($data as $key => $v) {
            $res[$v['playedGroup']][] = $v;
        }
        $codes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        $res2 = [];

        foreach ($poistion_count as $key => $v) {
            $dd = $res[$v['playedGroup']];
            $code = [];
            foreach ($dd as $key => $c) {
                if (count($code) <= 9) {
                    if (!in_array(intval($c['actionData']), $code)) {
                        array_push($code, intval($c['actionData']));
                    }
                } else {
                    break;
                }
            }
            $diff_code = array_diff($codes, $code);
            $res2[$v['playedGroup']] = $diff_code;
        }
        //多种组合选最优
        $total_list = [];
        for ($i = 0; $i <= 1000; $i++) {
            $total_res = $this->count_js_sylot($res, $res2, $codes);
            if (!in_array($total_res, $total_list)) {
                $total_list[] = $total_res;
            }
            if($total_res['money']==0) { //有最小值不用再循环了,也许可以小于100都行
                break;
            }
        }
        $total_list = sortByMultiCols($total_list, array('money' => SORT_ASC));
        $first_data = reset($total_list);
        $rand_lists = [];
        foreach($total_list as $key=>$v) {
            if($v['money']==$first_data['money']) {
                $rand_lists[] = $v;
            }
        }
        shuffle($rand_lists);
        $random_keys=array_rand($rand_lists,1);
        $total_data = $rand_lists[$random_keys];
        $opencode = $total_data['code'];
        var_dump($opencode);exit;
    }


    /*
     * function
     */
    public function count_js_sylot($res, $res2, $codes) {
        $poistions = [116, 117, 118, 119, 120, 121, 122, 123, 124, 125];
        $total_res = [];
        foreach ($res2 as $key => $v) {
            if (!empty($v)) {
                $v = array_values($v);
                shuffle($v);
                foreach ($v as $c) {
                    if (!in_array($c, $total_res)) {
                        $total_res[$key] = $c;
                        break;
                    }
                }
            } else { //全部数字都买了
                $db = $res[$key];
                krsort($db); //投注最小的中
                foreach ($db as $dk => $dv) {
                    if (!in_array(intval($dv['actionData']), $total_res)) {
                        $total_res[$key] = intval($dv['actionData']);
                        break;
                    }
                }
            }
        }
        $total_res_value = array_values($total_res);
        $diff_value = array_diff($codes, $total_res_value);
        if (empty($diff_value)) {
            $diff_value = array_values($diff_value);
        }
        shuffle($diff_value);
        $result = [];
        foreach ($poistions as $key => $v) {
            if (!empty($total_res[$v])) {
                $result[$v] = $total_res[$v];
            } else {
                $result[$v] = array_shift($diff_value);
            }
        }
        $count_money = $this->count_money($res, $result);
        return ['money' => $count_money, 'code' => implode(',', $result)];
    }

    public function count_money($res, $result) {
        $win_money = 0;
        foreach ($result as $key => $v) {
            $dd = $res[$key];
            foreach ($dd as $key => $c) {
                if ($c['actionData'] == $v) { //中奖
                    $win_money+=floatval($c['money']) * floatval($c['odds']);
                }
            }
        }
        return $win_money;
    }
    
    //修补函数(针对出错未结算的)
    public final function repairFun() {
        
    }
    
    
    //预设开奖接口
    public final function jsscbprizes() {
        $this->display('sylot/js/js_bprizes.php');
    }
    

}
